package com.basic.library.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class RViewHolder extends RecyclerView.ViewHolder {
	private SparseArray<View> mViews;
	private View mConvertView;
	private Context mContext;

	public RViewHolder(Context context, View itemView, ViewGroup parent) {
		super(itemView);
		mContext = context;
		mConvertView = itemView;
		mViews = new SparseArray<View>();
	}

	public static RViewHolder get(Context context, ViewGroup parent, int layoutId) {

		View itemView = LayoutInflater.from(context).inflate(layoutId, parent,
				false);
		RViewHolder holder = new RViewHolder(context, itemView, parent);
		return holder;
	}


	public View getConvertView()
	{
		return mConvertView;
	}
	/**
	 * 通过viewId获取控件
	 *
	 * @param viewId
	 * @return
	 */
	public <T extends View> T getView(int viewId) {
		View view = mViews.get(viewId);
		if (view == null) {
			view = mConvertView.findViewById(viewId);
			mViews.put(viewId, view);
		}
		return (T) view;
	}

	/**
	 * 为TextView设置字符�??
	 *
	 * @param viewId
	 * @param text
	 * @return
	 */
	public RViewHolder setText(int viewId, String text) {
		TextView view = getView(viewId);
		view.setText(text);
		return this;
	}

	/**
	 * 为ImageView设置图片
	 *
	 * @param viewId
	 * @param drawableId
	 * @return
	 */
	public RViewHolder setImageResource(int viewId, int drawableId) {
		ImageView view = getView(viewId);
		view.setImageResource(drawableId);

		return this;
	}
}
